package com.ruoyi.web.controller.shopping;


import java.util.List;

import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.shopping.domain.MallCoupons;
import com.ruoyi.shopping.service.IMallCouponsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.shopping.domain.MallCouponsMember;
import com.ruoyi.shopping.service.IMallCouponsMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券领取Controller
 *
 * @author ruoyi
 * @date 2022-06-09
 */
@RestController
@RequestMapping("/shopping/mallCouponsMember")
public class MallCouponsMemberController extends BaseController
{
    @Autowired
    private IMallCouponsMemberService mallCouponsMemberService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMallCouponsService couponsService;

    /**
     * 查询优惠券领取列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallCouponsMember mallCouponsMember)
    {
        startPage();
        List<MallCouponsMember> list = mallCouponsMemberService.selectMallCouponsMemberList(mallCouponsMember);

        return getDataTable(list);
    }

    /**
     * 导出优惠券领取列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:export')")
    @Log(title = "优惠券领取", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallCouponsMember mallCouponsMember)
    {
        List<MallCouponsMember> list = mallCouponsMemberService.selectMallCouponsMemberList(mallCouponsMember);
        ExcelUtil<MallCouponsMember> util = new ExcelUtil<MallCouponsMember>(MallCouponsMember.class);
        return util.exportExcel(list, "优惠券领取数据");
    }

    /**
     * 获取优惠券领取详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallCouponsMemberService.selectMallCouponsMemberById(id));
    }

    /**
     * 新增优惠券领取
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:add')")
    @Log(title = "优惠券领取", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCouponsMember mallCouponsMember)
    {
        //根据领取人id查询领取人信息
        Member member = memberService.selectMemberById(mallCouponsMember.getMemberId());
        MallCoupons mallCoupons = couponsService.selectMallCouponsById(mallCouponsMember.getCouponId());//根据优惠券id查询
        MallCouponsMember couponsMember = new MallCouponsMember(mallCoupons);
        couponsMember.setMemberId( member.getId());
        couponsMember.setMemberNickname( member.getNickName());
        couponsMember.setMemberPhoto(  member.getHeadImg());

        couponsMember.setReceiveType(DicConstants.RECEIVE_TYPE_Gift.getCode());
        mallCouponsMemberService.insertMallCouponsMember(couponsMember);
        return AjaxResult.success();
    }

    /**
     * 修改优惠券领取
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:edit')")
    @Log(title = "优惠券领取", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCouponsMember mallCouponsMember)
    {
        mallCouponsMember.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(mallCouponsMemberService.updateMallCouponsMember(mallCouponsMember));
    }

    /**
     * 删除优惠券领取
     */
    @PreAuthorize("@ss.hasPermi('shopping:mallCouponsMember:remove')")
    @Log(title = "优惠券领取", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallCouponsMemberService.logicDeleteMallCouponsMember(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
